package com.luo.demo.sharding.jdbc.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luo.demo.sharding.jdbc.mapper.ProductMapper;
import com.luo.demo.sharding.jdbc.model.entity.Product;
import com.luo.demo.sharding.jdbc.model.param.ProductListParam;
import com.luo.demo.sharding.jdbc.model.param.ProductPageParam;
import com.luo.demo.sharding.jdbc.model.result.CommonResult;
import com.luo.demo.sharding.jdbc.service.IProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author luohq
 * @since 2021-10-22
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    public static final Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);

    @Override
    public CommonResult<Product> findPage(ProductPageParam productPageParam) {
        logger.info("find product page, param: {}", productPageParam);
        this.page(
                productPageParam,
                Wrappers.<Product>lambdaQuery()
                        .in(!CollectionUtils.isEmpty(productPageParam.getProductIdList()), Product::getProductId, productPageParam.getProductIdList())
        );
        logger.info("find product page, result: {}", productPageParam);
        return CommonResult.successRows((int) productPageParam.getTotal(), productPageParam.getRecords());
    }

    @Override
    public CommonResult<Product> findList(ProductListParam productListParam) {
        logger.info("find product list, param: {}", productListParam);

        /** TODO 目前测试5.0.0-beta暂不支持in条件分片，故暂时调整为如下通过ID单独查询 */
//        this.list(
//                Wrappers.<Product>lambdaQuery()
//                        .in(!CollectionUtils.isEmpty(productPageParam.getProductIdList()), Product::getProductId, productPageParam.getProductIdList())
//        );
//        logger.info("find product list, result: {}", productPageParam);
//        return CommonResult.successRows((int) productPageParam.getTotal(), productPageParam.getRecords());


        List<Product> productList = this.baseMapper.selectList(productListParam);
        logger.info("find product list, result: {}", productList);
        return CommonResult.successRows(productList.size(), productList);
    }
}
